package com.junci.mtest.model.http;




import androidx.annotation.NonNull;


import com.junci.mtest.model.bean.MySelfInfo;
import com.junci.mtest.utils.LogUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * 尼莫
 * 这是~
 */
public class HttpClient {
    public final static String[] keys     ={"uid","device","version","chanel","app_id"};
    @NonNull
    public static List<String> getValues() {
        List<String> list = new ArrayList();
        list.add(0, MySelfInfo.getInstance().getId());
//        list.add(0,"111");
        list.add(1, "android");
        list.add(2, "");
        list.add(3, "");
        list.add(4, "");


        return list;
    }
    public static OkHttpClient defaultOkHttpClient() {
        // 这个拦截器给每个请求都添加POST参数mobileinfo
        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                RequestBody body1 = request.body();
                Request.Builder builder = request.newBuilder();
                List<String> values = getValues();
                {
                    MediaType mediaType = body1.contentType();
                    if (LogUtil.isLog()) {
                        LogUtil.s("底层数据。请求的类型：" + mediaType);
                    }
                    if (mediaType.toString().contains("multipart/form-data")) {
                        if (LogUtil.isLog()) {
                            LogUtil.s("是文件上传类型的");
                        }
                        MultipartBody body = (MultipartBody) body1;
                        int size = body.size();
                        MultipartBody.Builder builder1 = new MultipartBody.Builder();
                        builder1.setType(MediaType.parse("multipart/form-data"));
                        for (int i = 0; i < size; i++) {
                            builder1.addPart(body.part(i));
                        }
                        for (int i = 0; i < keys.length; i++) {
                            builder1.addFormDataPart(keys[i], values.get(i));
                        }
                        builder.method(request.method(), builder1.build());
                    }
                    {
                        FormBody.Builder newBody = new FormBody.Builder();

                        // 要在所有URL添加的POST参数
                        if (body1 instanceof FormBody) {
                            FormBody body = (FormBody) body1;
                            for (int i = 0; i < body.size(); i++) {
                                newBody.add(body.name(i), body.value(i));
                            }
                        } else if (body1 == null || body1.contentLength() == 0) {
                            newBody = new FormBody.Builder();
                        } else {
                            if (LogUtil.isLog()) {
                                LogUtil.s(" 不是FormBody 也不是没有Body~");
                            }
                        }

                        for (int i = 0; i < keys.length; i++) {
                            newBody.add(keys[i], values.get(i));
                        }
                        builder.method(request.method(), newBody.build());
                    }
                }
                return chain.proceed(builder.build());
            }
        };
        // log打印拦截器
        MyHttpLoggingInterceptor loggingInterceptor = new MyHttpLoggingInterceptor(new MyHttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (LogUtil.isLog()) {
//                    LogUtil.s("  OkHttp网络请求日志：" + StringUtil.replaceSpace(message));
//                    LogUtil.writeErrorToFile("  OkHttp网络请求日志："+message);
                }
            }
        }).setLevel(MyHttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .addInterceptor(interceptor)    // 添加请求参数的拦截器
                .addInterceptor(loggingInterceptor)
                .build();
    }
}
